import axios from "axios";
import qs from "qs";
import Vue from "vue";

const Util = {
	imgPath: 'http://127.0.0.1:8011/img/',
	apiPath: 'http://127.0.0.1:8010/'
};

const http = axios.create({
	baseURL: Util.apiPath,
	timeout: 5000,						// 超时
	transformRequest: [data => {		// 格式化请求数据
		if (data instanceof FormData) {
			return data;
		} else {
			return qs.stringify(data);
		}
	}],
});

// 返回拦截
http.interceptors.response.use(
	response => {
		if (response.data.status === 200) {
			return response.data;
		} else {
			return Promise.reject(response.data.msg);
		}
	}, 
	error => {
		return Promise.reject(error);
	}
);

// 添加示例方法
Vue.prototype.$http = http;